﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Catering.View_Operaction;

namespace Catering
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string temuname = Toolss.GetCookie("HYFZ_UName");
                string temppass = Toolss.GetCookie("HYFZ_UPass");
                if (!string.IsNullOrEmpty(temppass) && !string.IsNullOrEmpty(temuname))
                {
                    temppass = Bind_Data_to_Controls.Decode(temppass);
                    this.txt_name.Text = temuname;
                    this.txt_pwd.Attributes["value"] = temppass;
                    this.CheckBox1.Checked = true;
                }
            }
        }

        //登录
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            string str_name = this.txt_name.Text.Trim();
            string str_pwd = this.txt_pwd.Text.Trim();
            string pwd = str_pwd.GetMD5("");
            DataTable dt_user = new Catering_Rules.user().GetList("UName='" + str_name + "' and UPWD='" + pwd + "'").Tables[0];
            if (dt_user != null && dt_user.Rows.Count > 0)
            {
                int PersonnelID = Convert.ToInt32(dt_user.Rows[0]["PersonnelID"].ToString());
                Catering_Model.E_Personnel md = new Catering_Rules.T_Personnel().GetModel(PersonnelID);
                Session["GroupID"] = new Catering_Rules.ClassInfo().GetModel(md.ClassID).AreaID.ToString();
                Session["ClassID"] = md.ClassID.ToString();
                Session["User"] = md.Name.ToString();
                Session["UserID"] = md.id.ToString();
                Session["RoleID"] = dt_user.Rows[0]["URole"].ToString();
                Session["PersonnelID"] = dt_user.Rows[0]["ID"].ToString();

                string str = Session["UserID"].ToString();
                Session["key"] = str;
                Hashtable ht = (Hashtable)Application["UsersCount"];
                if (!ht.ContainsKey(str))
                {
                    ht.Add(str, md.Name + "," + DateTime.Now.ToString());
                }
                if (CheckBox1.Checked)
                {
                    Toolss.WriteCookie("HYFZ_UName", str_name, 24 * 60);
                    Toolss.WriteCookie("HYFZ_UPass", Bind_Data_to_Controls.Encode(str_pwd), 24 * 60);
                }
                else
                {
                    Toolss.WriteCookie("HYFZ_UName", str_name, -1);
                    Toolss.WriteCookie("HYFZ_UPass", Bind_Data_to_Controls.Encode(str_pwd), -1);
                }
                Toolss.WriteCookie("LoginUserID", md.id.ToString());
                Toolss.WriteCookie("LoginUserName", HttpContext.Current.Server.UrlEncode(md.Name));
                Response.Redirect("index.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户名或密码错误！')</script>");
            }
        }
    }
}